package com.kaishengit.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Goal;
import com.kaishengit.entity.GoalProgress;
import com.kaishengit.entity.GoalUserTask;
import com.kaishengit.entity.Task;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserTask;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.TaskService;
import com.kaishengit.service.UserService;

public class GoalServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	private GoalService service=new GoalService();
	private TaskService tservice=new TaskService();
	private UserService uservice=new UserService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String p=request.getParameter("pmothed");
		if(p==null||"".equals(p)){
			toGoal(request,response);
		}else if("new".equals(p)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toCreate(request,response);
			} else if("POST".equals(httpMethod)) {
				create(request,response);
			}
		}else if("edit".equals(p)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toEdit(request,response);
			} else if("POST".equals(httpMethod)) {
				edit(request,response);
			}
		}else if("alltask".equals(p)){
			toAll(request,response);
		}else if("deltask".equals(p)){
			toTaskdel(request,response);
		}else if("edittask".equals(p)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toTaskedit(request,response);
			} else if("POST".equals(httpMethod)) {
				taskedit(request,response);
			}
			
		}else if("mytask".equals(p)){
			toMyAll(request,response);
		}else if("totask".equals(p)){
			toTask(request,response);
		}else if("delgoal".equals(p)){
			delGoal(request,response);
		}
	}
	
	/**
	 * 删除目标
	 * @throws IOException 
	 * */
	private void delGoal(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String goalid=request.getParameter("id");
		service.del(goalid);
		response.sendRedirect("goal.jspx");
	}

	/**
	 * 根据浏览任务时进入对应任务页面
	 * @throws IOException 
	 * */
	private void toTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String taskid=request.getParameter("taskid");
		Task t=tservice.findTById(taskid);
		String goalid=t.getT_goal_id();
		response.sendRedirect("task.jspx?id="+goalid);
	}


	private void taskedit(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String taskid=request.getParameter("id");
		String name=request.getParameter("name");
		String starttime=request.getParameter("start");
		String endtime=request.getParameter("end");
		String desc=request.getParameter("desc");
		String isImport=request.getParameter("isImport");
		String progress=request.getParameter("progress");
		tservice.update(taskid,name,starttime,endtime,desc,isImport,progress);
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		List<UserTask> userTasklist=tservice.findByProjectId(projectid);
		request.setAttribute("userTasklist", userTasklist);
		request.getRequestDispatcher("WEB-INF/views/allTask.jsp").forward(request, response);
	}


	/**
	 * 任务进行编辑页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	
	private void toTaskedit(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String taskid=request.getParameter("id");
		Task task=tservice.findByTaskId(taskid);
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		List<User> userlist=uservice.findAllByProjectId(projectid);
		request.setAttribute("task", task);
		request.setAttribute("userList", userlist);
		request.getRequestDispatcher("WEB-INF/views/taskedit.jsp").forward(request, response);
		
	}



	/**
	 * 对任务进行删除
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toTaskdel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String taskid=request.getParameter("id");
		tservice.del(taskid);
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		List<UserTask> userTasklist=tservice.findByProjectId(projectid);
		request.setAttribute("userTasklist", userTasklist);
		request.getRequestDispatcher("WEB-INF/views/allTask.jsp").forward(request, response);
	}
	
	
	/**
	 * 进入到显示所有任务的页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		List<UserTask> userTasklist=tservice.findByProjectId(projectid);
		request.setAttribute("userTasklist", userTasklist);
		request.getRequestDispatcher("WEB-INF/views/allTask.jsp").forward(request, response);
		
	}
	
	/**
	 * 进入到显示我的所有任务的页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toMyAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		User user=(User) session.getAttribute("user");
		String userid=user.getId();
		List<UserTask> userTasklist=tservice.findByUserId(userid);
		request.setAttribute("userTasklist", userTasklist);
		request.getRequestDispatcher("WEB-INF/views/allTask.jsp").forward(request, response);
		
	}

	/**
	 * 提交修改goal的表单
	 * @throws IOException 
	 * **/

	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String goalid=request.getParameter("id");
		String name=request.getParameter("name");
		String desc=request.getParameter("desc");
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		service.update(goalid,name,desc,projectid);
		response.sendRedirect("goal.jspx");
	}
	
	
	/**
	 * 进入到修改goal页面
	 * @throws IOException 
	 * @throws ServletException 
	 * **/
	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String goalid=request.getParameter("id");
		Goal goal=service.findByGoalId(goalid);
		request.setAttribute("goal", goal);
		request.getRequestDispatcher("WEB-INF/views/edit_goal.jsp").forward(request, response);
	}
	

	/**
	 * 提交创建goal的表单
	 * @throws IOException 
	 * */
	
	private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name=request.getParameter("name");
		String desc=request.getParameter("desc");
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		User user=(User) session.getAttribute("user");
		String userid=user.getId();
		service.save(name,desc,projectid,userid);
		response.sendRedirect("goal.jspx");
	}
	
	/**
	 * 进入到创建goal表单的页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toCreate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/new_goal.jsp").forward(request, response);
	}

	/**
	 *进入到goal.jsp页面
	 * */
	
	private void toGoal(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		GoalService goalService=new GoalService();
		List<GoalProgress> list=goalService.findAllByprojectId(projectid);
		request.setAttribute("goalProgressList", list);
		request.getRequestDispatcher("WEB-INF/views/goal.jsp").forward(request, response);
	}
}
